//
//  JSTPIncomeTaxViewController.swift
//  JSTech
//
//  Created by 高得华 on 2020/10/9.
//  Copyright © 2020 华&梅. All rights reserved.
//

import UIKit

class JSTPIncomeTaxViewController: MDHViewController {
    
    ///重写父类的属性
    override var viewModel: JSTPIncomeTaxViewModel {
        get {
            return super.viewModel as! JSTPIncomeTaxViewModel
        }
    }
    
    ///背景视图
    fileprivate weak var viewScrollBack: UIScrollView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let view_back = UIScrollView.init(frame: self.view.bounds)
        self.view.addSubview(view_back)
        self.viewScrollBack = view_back
        MDHViewModel.adjustsScrollViewInsets_Never(view_back)
        
        // Do any additional setup after loading the view.
        ///点击按钮
        let button_zxbz = MDHSwiftHeader.navTitle(title: "上海市五险一金执行标准", color: UIColor.blue, font: kFont_PingFangSCSemibold(13), left: false)
        view_back.addSubview(button_zxbz)
        button_zxbz.mas_makeConstraints { (make) in
            MDHSwiftHeader.makeViewNavTopPath(make: make, top: 15)
            make?.left.equalTo()(15)
            make?.height.equalTo()(14)
        }
        button_zxbz.rac_signal(for: .touchUpInside)
            .deliverOnMainThread().subscribeNext { [weak self] (x) in
                if let _ = self {
                    let url = "http://rsj.sh.gov.cn/tfkcx_17604/20200617/t0035_1368737.html"
                    AppDelegate.pushToH5Vc(url: url)
                }
        }
        
        ///执行标准
        let label_wenben = UILabel()
        view_back.addSubview(label_wenben)
        label_wenben.text = "目前城镇职工社保缴费比例单位部分：养老20％，医保10％，失保1％，生育：1％，工伤按基础费率（1-8类）；个人部分：养老8％，医保2％、失保1％。\n上海市人力资源和社会保障局"
        label_wenben.textColor = kColor_MDH333333()
        label_wenben.font = kFont_PingFangSCMedium(13)
        label_wenben.numberOfLines = 0
        label_wenben.mas_makeConstraints { (make) in
            make?.left.equalTo()(button_zxbz)
            make?.top.equalTo()(button_zxbz.mas_bottom)?.offset()(5)
            make?.centerX.equalTo()(0)
        }
        
        /// 个人所得税税率表2020
        let button_gssmb = MDHSwiftHeader.navTitle(title: "个人所得税税率表2020", color: UIColor.blue, font: kFont_PingFangSCSemibold(13), left: false)
        view_back.addSubview(button_gssmb)
        button_gssmb.mas_makeConstraints { (make) in
            make?.top.equalTo()(label_wenben.mas_bottom)?.offset()(5)
            make?.left.equalTo()(15)
            make?.height.equalTo()(14)
        }
        button_gssmb.rac_signal(for: .touchUpInside)
            .deliverOnMainThread().subscribeNext { [weak self] (x) in
                if let _ = self {
                    let url = "https://www.gerensuodeshui.cn/"
                    AppDelegate.pushToH5Vc(url: url)
                }
        }
        
        /// 图片说明
        let imv_gssmb = UIImageView(image: UIImage(named: "geshuishuoming"))
        view_back.addSubview(imv_gssmb)
        //imv_gssmb.contentMode = .scaleAspectFill
        //imv_gssmb.clipsToBounds = true
        imv_gssmb.mas_makeConstraints { (make) in
            make?.left.equalTo()(button_gssmb)
            make?.top.equalTo()(button_gssmb.mas_bottom)?.offset()(5)
            make?.height.equalTo()(64)
            make?.centerX.equalTo()(0)
        }
        MDHSwiftHeader.addTap(view: imv_gssmb) { (tap) in
            if let ima = UIImage(named: "geshuishuoming") {
                kMDHPhotoBM().showPhoto([ima], index: 0)
            }
        }
        
        /// 个人所得税的 说明
        let label_wenben1 = UILabel()
        view_back.addSubview(label_wenben1)
        label_wenben1.textColor = kColor_MDH333333()
        label_wenben1.font = kFont_PingFangSCMedium(13)
        label_wenben1.numberOfLines = 0
        label_wenben1.mas_makeConstraints { (make) in
            make?.left.equalTo()(button_zxbz)
            make?.top.equalTo()(imv_gssmb.mas_bottom)?.offset()(5)
            make?.centerX.equalTo()(0)
        }
        label_wenben1.text = """
        累计预扣预缴个人所得税计算公式2020
        累计预扣预缴应纳税所得额=累计收入-累计免税收入-累计减除费用-累计专项扣除-累计专项附加扣除-累计依法确定的其他扣除

        本期应预扣预缴税额=（累计预扣预缴应纳税所得额×预扣率-速算扣除数)-累计减免税额-累计已预扣预缴税额

        其中：累计减除费用，按照个税起征点5000元/月乘以纳税人当年截至本月在本单位的任职受雇月份数计算。
        """
        
        ///工资、薪金所得(月薪)
        let label_wenben2 = UILabel()
        view_back.addSubview(label_wenben2)
        label_wenben2.textColor = UIColor.red
        label_wenben2.font = kFont_PingFangSCMedium(13)
        label_wenben2.text = "工资、薪金所得(月薪)"
        label_wenben2.mas_makeConstraints { (make) in
            make?.left.equalTo()(button_zxbz)
            make?.top.equalTo()(label_wenben1.mas_bottom)?.offset()(5)
            make?.centerX.equalTo()(0)
        }
        
        ///
        let arr_texts = ["纳税期数", "本月工资", "累计工资", "各项社会保险费", "累计", "专项附加扣除", "累计"]
        ///距离顶部的视图
        var top_text_view: UIView = label_wenben2
        for (i, text) in arr_texts.enumerated() {
            let tf_1 = JSTPayTaxesView()
            self.view.addSubview(tf_1)
            tf_1.labelLeft?.text = text
            tf_1.mas_makeConstraints { (make) in
                make?.left.equalTo()(5)
                make?.top.equalTo()(top_text_view.mas_bottom)?.offset()(5)
                make?.height.equalTo()(32)
                make?.width.equalTo()(self.view.mas_width)?.multipliedBy()(0.5)
            }
            top_text_view = tf_1
            if i == 0 {
                tf_1.labelRight?.isHidden = true
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.viewScrollBack?.contentSize = CGSize(width: self.view.mh_width, height: self.view.mh_height+10)
    }
}


///文本 纳税文本
class JSTPayTaxesView: UIView, UITextFieldDelegate {
    
    ///左侧文本
    weak var labelLeft: UILabel?
    ///右侧文本
    weak var labelRight: UILabel?
    ///文本输入框
    weak var textFiled: UITextField?
    
    ///block的返回输入的文本
    var blockText: VoidBlock_string?
    ///是否是 纳税期数
    var is_number = false
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        ///右侧的文本
        let label_r = UILabel()
        self.addSubview(label_r)
        self.labelRight = label_r
        label_r.text = "元"
        label_r.textColor = kColor_MDH666666()
        label_r.font = kFont_PingFangSCRegular(13)
        label_r.mas_makeConstraints { (make) in
            make?.right.equalTo()(-2)
            make?.centerY.equalTo()(0)
            make?.width.equalTo()(20)
        }
        
        ///文本的输入框
        let tf_wb = UITextField()
        self.addSubview(tf_wb)
        self.textFiled = tf_wb
        tf_wb.delegate = self
        tf_wb.font = kFont_PingFangSCMedium(14)
        tf_wb.textColor = kColor_MDH333333()
        tf_wb.borderColor = UIColor.orange
        tf_wb.borderWidth = 1
        tf_wb.dh_cornerRadius = 2
        tf_wb.mas_makeConstraints { (make) in
            make?.right.equalTo()(label_r.mas_left)?.offset()(-3)
            make?.centerY.equalTo()(0)
            make?.height.equalTo()(30)
            make?.width.equalTo()(72)
        }
        let view_left = UIView(frame: CGRect(x: 0, y: 0, width: 3, height: 30))
        tf_wb.leftView = view_left
        tf_wb.leftViewMode = .always
        
        ///左侧的文本
        let label_l = UILabel()
        self.addSubview(label_l)
        self.labelLeft = label_l
        label_l.textColor = kColor_MDH666666()
        label_l.font = kFont_PingFangSCRegular(12)
        label_l.textAlignment = .right
        label_l.mas_makeConstraints { (make) in
            make?.right.equalTo()(tf_wb.mas_left)?.offset()(-3)
            make?.centerY.equalTo()(0)
            make?.left.equalTo()(2)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///即将输入的时候
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        
        return true
    }
}
